<!--  -->
<template>
  <div class="comment-container">
    <el-card>
      <div slot="header">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-table :data="articles"
                style="width: 100%"
                class="table-list"
                stripe>
        <el-table-column prop="title"
                         label="标题">
        </el-table-column>
        <el-table-column prop="total_comment_count"
                         label="总评论数">
        </el-table-column>
        <el-table-column prop="fans_comment_count"
                         label="粉丝评论数">
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{scope.row.comment_status?'正常':'关闭'}}
          </template>
        </el-table-column>
        <el-table-column prop="date"
                         label="操作">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.comment_status"
                       active-color="#13ce66"
                       inactive-color="#ff4949"
                       @change="onStatusChange(scope.row)"
                       :disabled="scope.row.statusDisabled">
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page.sync="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :page-size.sync="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     background
                     :total="totalCount" />
    </el-card>
  </div>
</template>

<script>
import { getArticle } from '@/api/article'
import { changeStatus } from '@/api/comment'
export default {
  name: 'CommentIndex',
  components: {},
  props: {},
  data () {
    return {
      articles: [],
      totalCount: 0,
      pageSize: 10,
      page: 1
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadArticle()
  },
  mounted () { },
  methods: {
    handleSizeChange (val) {
      this.loadArticle(1)
    },
    handleCurrentChange (val) {
      this.loadArticle(val)
    },
    loadArticle (page = 1) {
      // 让分页组件激活的页码和请求数据的页码操持一致
      this.page = page
      getArticle(
        {
          response_type: 'comment',
          page,
          per_page: this.pageSize
        }
      ).then(res => {
        const { results } = res.data.data
        results.forEach(article => {
          article.statusDisabled = false
        })
        this.articles = results
        this.totalCount = res.data.data.total_count
      })
    },
    onStatusChange (article) {
      // 禁用开关
      article.statusDisabled = true
      changeStatus(article.id.toString(), article.comment_status).then(res => {
        console.log(res)
        article.statusDisabled = false
        this.$message({
          type: 'success',
          message: article.comment_status ? '开启文章评论功能' : '关闭文章评论功能'
        })
      })
    }
  }
}
</script>

<style lang='less' scoped>
.table-list {
  margin-bottom: 30px;
}
</style>
